package eventservice;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;

public class ListEventPlace implements Iterable<InscriptionEvent>,Serializable {
	
	
	private static final long serialVersionUID = 1L;
	
	private Hashtable<String,EventAbs > eTable = new Hashtable<String,EventAbs >();
	
	private static ListEventPlace instance;
	
	private ListEventPlace(){}
	
	/**
	 * Retourne une seule et meme instance de la liste d'event place<br>
	 * Si la liste n'a pas ete encore instanciee, une nouvelle instance sera faite.<br>
	 * @return l'instance de la liste
	 */
	public static ListEventPlace getInstance(){
		
			if (instance == null)
				instance = new ListEventPlace();
			return instance;
	}
	
	/**
	 * remove an event to the table
	 * @param n
	 */
	public void removeEventFromTable(String n){
		this.eTable.remove(n);
	}
	
	/**
	 * add an event to the table
	 * @param n
	 * @param e
	 */
	public void addEventToTable(String n, EventAbs e){
		synchronized(eTable){
			this.eTable.put(n, e);
		}
		
	}
	
	/**
	 * Indicates if the event is in the table
	 * @param name
	 * @return True if there's. Otherwise, false
	 */
	public boolean isEventInTable(String name){
		boolean response = false;
		InscriptionEvent event = (InscriptionEvent) this.eTable.get(name);
		if(event !=null)
			response = true;
		
		return response;
	}
	
	/**
	 * Verified if the event is in tables
	 * @param noEvent
	 * @return
	 */
	public boolean verifierInscriptionEventByNoID(int noEvent) {
		boolean reponse = false;
		Enumeration<EventAbs > enume = this.eTable.elements();
		while(enume.hasMoreElements()){
			InscriptionEvent eventp = (InscriptionEvent) enume.nextElement();
			if(eventp.getNoID() ==noEvent){
				reponse = true;
				break;
			}
		}
		
		
		return reponse;
		
	}
	
	/**
	 * 
	 * @param name
	 * @return
	 */
	public boolean verifiedInscriptionEventByName(String name){
		boolean response  = true;
		InscriptionEvent iEvent = (InscriptionEvent) geteTable().get(name);
		if(iEvent == null){
			response = false;
		}
   	return response;
		//return false;
	}
	
	/**
	 * Verified if there's still some place left for an event
	 * @param name
	 * @return
	 */
	public boolean verifiedIfPlaceAvailableByEventName(String name){
		
		InscriptionEvent iEvent = (InscriptionEvent) geteTable().get(name);
		if(iEvent != null){
			return iEvent.isAvailability();
		}
		return false;
	}
	
	
	
	/**
	 * Get the event with full informations
	 * @param name
	 * @return
	 */
	public InscriptionEvent getInscriptionEventByName(String name){
		return (InscriptionEvent) this.eTable.get(name);
			
	}
	
	/**
	 * Return a concrete reservation event inscription
	 * @param name
	 * @return
	 */
	public ConcreteEvent getEvent(String name){
		InscriptionEvent iEvent = (InscriptionEvent) this.eTable.get(name);
		return iEvent.getEvent();
		
	}
	

	/**
	 * @return the eTable
	 */
	public Hashtable<String, EventAbs> geteTable() {
		return eTable;
	}

	/**
	 * @param eTable the eTable to set
	 */
	public void seteTable(Hashtable<String, EventAbs> eTable) {
		this.eTable = eTable;
	}

	@Override
	public Iterator<InscriptionEvent> iterator() {
		// TODO Auto-generated method stub
		return null;
	}

}
